import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xrenovation/services/locale_service.dart';

class LanguageSwitcher extends StatelessWidget {
  const LanguageSwitcher({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final localeService = Provider.of<LocaleService>(context, listen: true);

    return PopupMenuButton<Locale>(
      icon: Icon(Icons.language, color: theme.colorScheme.onSurface),
      itemBuilder:
          (context) => [
            for (final locale in LocaleService.supportedLocales)
              PopupMenuItem<Locale>(
                value: locale,
                child: Row(
                  children: [
                    Icon(
                      Icons.check,
                      size: 20,
                      color: locale == localeService.locale ? theme.primaryColor : Colors.transparent,
                    ),
                    const SizedBox(width: 12),
                    Text(_getLanguageName(locale), style: theme.textTheme.bodyMedium),
                  ],
                ),
              ),
          ],
      onSelected: (newLocale) async {
        await localeService.setLocale(newLocale);
      },
    );
  }

  String _getLanguageName(Locale locale) {
    switch (locale.languageCode) {
      case 'zh':
        return '中文';
      case 'en':
      default:
        return 'English';
    }
  }
}
